<?php
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];
require( $this_file . 'wp-blog-header.php' );
if( !wp_verify_nonce( $_POST[ 'sofa_suppastore_cc' ], 'chck_ref_cc' ) ) {
	echo '<p>' . __( "Permissions Error!", "sofa_suppastore" ) . '</p>';
	exit;
}
global $wpdb;
$aOptions      = SofaSuppaStore::initOptions( false );
$entrycurrency = $aOptions[ 'entrycurrency' ];
$google_fonts  = $aOptions[ 'google_fonts' ];
$currency_code = $aOptions[ 'paypalcurrency' ];
$checkout_tax  = floatval( $aOptions[ 'checkout_tax' ] );
if( $checkout_tax < 0 ) $checkout_tax = 0;
$owner = get_userdata( 1 ); // to admin
$to_admin = $owner->user_email;
$report_message = '';
$output_userdata = '';
$entire_name = '';
$payer_email = '';
$contact_phone = '';
$address_street = '';
$address_zip = '';
$address_city = '';
$entire_country = '';
$payment_amount = '';
$discount_perc = 0;
// user
$output_userdata .= __( "USER DETAILS", "sofa_suppastore" ) . '<br />';
$output_userdata .= '----------------------------------------<br />';
foreach( $_POST as $key => $value ) {
	if( $key == 'ab_firstname' ) {
		$output_userdata .= __( "First Name", "sofa_suppastore" ) . ': ' . $value . '<br />';
		$entire_name .= $value . ' ';
	}
	if( $key == 'ab_lastname' ) {
		$output_userdata .= __( "Last Name", "sofa_suppastore" ) . ': ' . $value . '<br />';
		$entire_name .= $value;
	}
	if( $key == 'ab_cellphone' ) {
		$output_userdata .= __( "Cell Phone", "sofa_suppastore" ) . ': ' . $value . '<br />';
		$contact_phone .= $value . ' | ';
	}
	if( $key == 'ab_phone' ) {
		$output_userdata .= __( "Phone Number", "sofa_suppastore" ) . ': ' . $value . '<br />';
		$contact_phone .= $value;
	}
	if( $key == 'ab_email' ) {
		$output_userdata .= __( "E-mail Address", "sofa_suppastore" ) . ': ' . $value . '<br /><br />';
		$payer_email = $value;
	}
}
// shipping
$shipping_and_handling_global = 0;
$shipping_and_handling_country = 0;
$output_userdata .= __( "SHIPPING DETAILS", "sofa_suppastore" ) . '<br />';
$output_userdata .= '----------------------------------------<br />';
foreach( $_POST as $key => $value ) {
	if( $key == 'ab_address' ) {
		$output_userdata .= __( "Shipping Address", "sofa_suppastore" ) . ': ' . $value . ' ';
		$address_street .= $value . ' ';
	}
	if( $key == 'ab_address2' ) {
		$output_userdata .= $value . '<br />';
		$address_street .= $value;
	}
	if( $key == 'ab_zipcode' ) {
		$output_userdata .= __( "ZIP Code", "sofa_suppastore" ) . ': ' . $value . '<br />';
		$address_zip .= $value;
	}
	if( $key == 'ab_city' ) {
		$output_userdata .= __( "City", "sofa_suppastore" ) . ': ' . $value . '<br />';
		$address_city .= $value;
	}
	if( $key == 'ab_country' ) {
		$arr_country_pipe = explode( '~', $value ); // 0 - country, 1 - shipping and handling price
		$shipping_and_handling_country = $arr_country_pipe[ 0 ];
		$shipping_and_handling_global = number_format( $arr_country_pipe[ 1 ], 2, '.', '' );
		$output_userdata .= __( "Country", "sofa_suppastore" ) . ': ' . $shipping_and_handling_country . '<br /><br />';
		$entire_country .= $value;
	}
	// discount
	if( $key == 'ab_discount' ) $discount_perc = floatval( $value );
}
// basket
$output_userdata .= __( "ORDERED ITEMS", "sofa_suppastore" ) . '<br />';
$output_userdata .= '----------------------------------------<br />';
if( isset( $_COOKIE[ 'sofasuppastore' ] ) ) {	
	$ppal_custom     = '';
	$items_sum       = 0;
	$shipping_sum    = $shipping_and_handling_global;
	$total_sum       = 0;
	$discount_sum    = 0;
	$bare_tax        = 0;
	$output_userdata = '';	
	foreach( $_COOKIE[ 'sofasuppastore' ] as $name => $value ) {
		$single_item_arr   = explode( '~', $value ); // 0 - name (ID), 1 - size, 2 - quantity, 3 - price, 4 - selected row, 5 - stock, 6 - shipp		
		$single_item_name  = get_the_title( intval( $single_item_arr[ 0 ] ) ); // make prod title shorter
		$single_item_id    = intval( $single_item_arr[ 0 ] ); // product id
		$single_item_size  = $single_item_arr[ 1 ]; // product size
		$single_item_qty   = intval( $single_item_arr[ 2 ] ); // quantity
		$single_item_price = number_format( $single_item_arr[ 3 ], 2, '.', '' ); // single price
		$single_item_shipp = ( $single_item_arr[ 6 ] ) ? number_format( $single_item_arr[ 6 ], 2, '.', '' ) : 0; // shipping price		
		$output_userdata .= ( __( "Product ID", "sofa_suppastore" ) . ': ' . $single_item_id . '<br />' );
		$output_userdata .= ( __( "Product Name", "sofa_suppastore" ) . ': ' . $single_item_name . '<br />' );
		$output_userdata .= ( __( "Size", "sofa_suppastore" ) . ': ' . $single_item_size . '<br />' );
		$output_userdata .= ( __( "Quantity", "sofa_suppastore" ) . ': ' . $single_item_qty . '<br />' );
		$output_userdata .= ( __( "Item Price", "sofa_suppastore" ) . ': ' . number_format( $single_item_price, 2, '.', '' ) . '<br />' );
		$calc_item_total = $single_item_qty * $single_item_price;
		$output_userdata .= ( __( "Item(s) Total", "sofa_suppastore" ) . ': ' . number_format( $calc_item_total, 2, '.', '' ) . '<br />' );
		$calc_ship_total = 0;		
		if( $single_item_shipp > 0 ) {
			$calc_ship_total = $single_item_qty * $single_item_shipp;
			$output_userdata .= ( __( "Shipping Price x Qty", "sofa_suppastore" ) . ': ' . number_format( $calc_ship_total, 2, '.', '' ) . '<br />' );
			$shipping_sum = $shipping_sum + $calc_ship_total;
		}
		if( $discount_perc > 0 ) {
			$item_discount = $calc_item_total * ( $discount_perc / 100 );
			$discount_sum = $discount_sum + $item_discount;
		}		
		if( $calc_ship_total == 0 ) $calc_ship_total = '0.00';
		$ppal_custom .= ( $single_item_id . '~' . $single_item_name . '~' . $single_item_size . '~' . $single_item_qty . '~' . $single_item_price . '~' . $calc_ship_total . '^' );		
		$items_sum = $items_sum + $calc_item_total;		
		$output_userdata .= '----------------------------------------<br />';
	}
	$output_userdata .= '----------------------------------------<br />';
	if( $checkout_tax > 0 ) {
		$bare_tax = $items_sum * ( $checkout_tax / 100 );
		$total_sum = $total_sum + $bare_tax;
	}
	// discount aka coupon code or voucher
	if( $discount_perc > 0 && $discount_sum > 0 ) {
		$output_userdata .=  __( "Discount", "sofa_suppastore" ) . '(' . $discount_perc . '%): ' . $entrycurrency . ' -' . number_format( $discount_sum, 2, '.', ',' ) . '<br />';
		$items_sum = $items_sum - $discount_sum;
	}
	$total_sum = $items_sum + $shipping_sum;
	$output_userdata .=  __( "Subtotal", "sofa_suppastore" ) . ': ' . $entrycurrency . ' ' . number_format( $items_sum, 2, '.', ',' ) . '<br />';
	$output_userdata .= __( "Shipping/Handling", "sofa_suppastore" ) . ': ' . $entrycurrency . ' ' . number_format( $shipping_sum, 2, '.', ',' ) . '<br />';
	if( $checkout_tax > 0 ) {
		$bare_tax = $items_sum * ( $checkout_tax / 100 );
		$output_userdata .=  __( "Tax", "sofa_suppastore" ) . '(' . $checkout_tax . '%): ' . $entrycurrency . ' ' . number_format( $bare_tax, 2, '.', ',' ) . '<br />';
		$total_sum = $total_sum + $bare_tax;
	}
	$output_userdata .= '----------------------------------------<br />';
	$output_userdata .= __( "Total", "sofa_suppastore" ) . ': ' . $entrycurrency . ' ' . number_format( $total_sum, 2, '.', ',' ) . '<br />';
}
// save data to a DB
$payment_date = current_time( 'mysql' );
$txn_id = time();
$payer_status = __( "Mail Order", "sofa_suppastore" );
$payment_amount = number_format( $total_sum, 2, '.', ',' );
$bare_tax = number_format( $bare_tax, 2, '.', ',' );
$discount_sum = number_format( $discount_sum, 2, '.', ',' );
$Q = $wpdb->insert( ( $wpdb->prefix . 'purchases_history' ), array( 'trans_date' => $payment_date, 'unique_code' => $txn_id, 'trans_status' => $payer_status, 'trans_basket' => $ppal_custom, 'user_name' => $entire_name, 'user_email' => $payer_email, 'user_phone' => $contact_phone, 'user_address' => $address_street, 'user_zip' => $address_zip, 'user_city' => $address_city, 'user_country' => $entire_country, 'currency' => $currency_code, 'gross' => $payment_amount, 'discount' => $discount_sum, 'handling' => $shipping_and_handling_global, 'tax' => $bare_tax, 'flag_shipped' => 0 ), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d' ) );
// clear cookies?
foreach( $_COOKIE[ 'sofasuppastore' ] as $name => $value ) {
	$itemid = $name;
	setcookie( "sofasuppastore[$itemid]", "", time()-36000, '/' );
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
<head profile="http://gmpg.org/xfn/11">
	<?php
	$font_str = $google_fonts;
	$font_str_arr = explode( ':', $font_str );
	$font_name = $font_str_arr[ 0 ];
	$clean_font_arr = explode( '+', $font_name );
	$clean_font_name = implode( ' ', $clean_font_arr );
    ?>
    <link href='http://fonts.googleapis.com/css?family=<?php echo $google_fonts; ?>' rel='stylesheet' type='text/css' />
    <link rel="stylesheet" href="<?php echo( get_template_directory_uri() . '/960.css' ); ?>" type="text/css" media="screen, projection" />
    <link rel="stylesheet" href="<?php echo( get_template_directory_uri() . '/screen.css' ); ?>" type="text/css" media="screen, projection" />
    <style type="text/css">
	/* handle basic styles according to google fonts selection */
	h1, h2, h3, h4, h5, h6 { font-family: '<?php echo $clean_font_name; ?>', Arial, sans-serif; font-weight: normal; }
	</style>
	<link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>" type="text/css" media="screen, projection" />
</head>
<body>
<div id="lightcontent" class="container_8 clearfix">
    <div id="featured" class="castshadow grid_8">
        <div class="grid_8 alpha omega">
        	<h2 class="grid_8 alpha omega"><?php _e( "All righty!", "sofa_suppastore" ); ?></h2>
        </div>
        <div id="proddetails" class="grid_8 alpha omega">
<?php
// cookie must exist, otherwise page reload would cause double order
if( $Q && isset( $_COOKIE[ 'sofasuppastore' ] ) ) {
	// append to message
	$report_message .= __( "Your Order has been received!", "sofa_suppastore" ) . '<br />';
	// send notification
	$subject = __( "Purchase made (e-mail order)", "sofa_suppastore" ) . ', ' . get_bloginfo( 'name' );
	$headers = "Content-Type: text/html; charset=utf-8; format=flowed\r\n"
	. "Content-Transfer-Encoding: 8bit\r\n"
	. "MIME-Version: 1.0\r\n"
	. "From: $to_admin\r\n"
	. "Reply-To: $to_admin\r\n"
	. "Return-Path: $to_admin\r\n";
	$message = __( "Just to let you know that your Order has been received. Here are the details:", "sofa_suppastore" );
	$message .= '<br /><br />';
	$message .= $output_userdata;
	$message .= '<br /><br />';
	$message .= __( "Thank you very much!", "sofa_suppastore" ) . '<br />' . home_url();
	// send mail to buyer
	if( @mail( $payer_email, $subject, $message, $headers ) ) {
		$report_message .= __( "You may check you mailbox, confirmation message with purchase details has been sent too!", "sofa_suppastore" ) . '<br />';
	} else {
		$report_message .= __( "Confirmation message with purchase details can not be sent, mail server error!", "sofa_suppastore" ) . '<br />';
	}
	// send mail to admin
	// $s_adm_arr = explode( '@', $to_admin );
	// $fake_email = 'system@' . $s_adm_arr[ 1 ];
	$subject_admin = __( "New Order received (e-mail order)", "sofa_suppastore" ) . ', ' . get_bloginfo( 'name' );
	$headers_admin = "Content-Type: text/html; charset=utf-8; format=flowed\r\n"
	. "Content-Transfer-Encoding: 8bit\r\n"
	. "MIME-Version: 1.0\r\n"
	. "From: $to_admin\r\n"
	. "Reply-To: $to_admin\r\n"
	. "Return-Path: $to_admin\r\n";
	$message_admin = __( "Just to let you know that new Order has been placed. Here are the details:", "sofa_suppastore" );
	$message_admin .= '<br /><br />';
	$message_admin .= $output_userdata;
	@mail( $to_admin, $subject_admin, $message_admin, $headers_admin );
} else {
	// append to message
	$report_message .= __( "Your Order can not be processed, service responded with error!", "sofa_suppastore" ) . '<br />';
}
echo '<p>' . $report_message . '</p>';
?>
		</div>
    </div>
</div>
</body>
</html>